package com.yllt4cloud.common.ddbase.constant.entity;



import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author nelson
 */
public class SessionEntity  implements Serializable {

    private String encryptKey;
    private CryptEnum cryptEnum;
    private UserInfo userInfo;
    private Content content;
    public static class Content{
        private String userName;
        private String token;
        private String user;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }
    }

    public Content getContent() {
        return content;
    }

    public void setContent(Content content) {
        this.content = content;
    }

    //        content.put("userName", loginUser.getLoginName());
//        content.put("token", token);
//        content.put("user", sessionKey);
    //存放于session中，用户请求时会附带token，做一致性验证
    private String token;
    //上一次操作时间
    private Date lastTime;
    private Long liveTime;

    public SessionEntity() {
    }

    private Set<String> privilegeSets  = new HashSet<String>(0);

    public boolean canEncrypt(){
        return !StringUtils.isBlank(encryptKey)&&null!= cryptEnum;
    }

    //默认有效期30分钟
    public static final Long DEFAULT_LIVE_TIME = 30l;

    public String getEncryptKey() {
        return encryptKey;
    }

    public CryptEnum getCryptEnum() {
        return cryptEnum;
    }

    public void setCryptEnum(CryptEnum cryptEnum) {
        this.cryptEnum = cryptEnum;
    }

    public void setEncryptKey(String encryptKey) {
        this.encryptKey = encryptKey;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Set<String> getPrivilegeSets() {
        return privilegeSets;
    }

    public void setPrivilegeSets(Set<String> privilegeSets) {
        this.privilegeSets = privilegeSets;
    }

    public Date getLastTime() {
        return lastTime;
    }

    public void setLastTime(Date lastTime) {
        this.lastTime = lastTime;
    }

    public Long getLiveTime() {
        if(liveTime == null)
            return DEFAULT_LIVE_TIME;
        return liveTime;
    }

    public void setLiveTime(Long liveTime) {
        this.liveTime = liveTime;
    }


    public boolean isExpired(){
        long time = new Date().getTime() - lastTime.getTime();
        //判断是否大于有效时间
        if(time >= getLiveTime() * 60 * 1000){
            return false;
        }

        return true;
    }

}
